//
//  IWTabBarController.m
//  8期微博
//
//  Created by apple on 14-8-31.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "TTTabBarController.h"
#import "TTHomeViewController.h"
#import "TTMessageViewController.h"
#import "TTDiscoverViewController.h"
#import "TTProfileViewController.h"
#import "TTImageTool.h"
#import "TTTabBar.h"
#import "TTNavigationController.h"

@interface TTTabBarController ()<IWTabBarDelegate>
@property (nonatomic, weak) TTTabBar *customTabBar;
@end

@implementation TTTabBarController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        // 1.首页
        TTHomeViewController *home = [[TTHomeViewController alloc] init];
//        home.view.backgroundColor = IWRandomColor;
        [self addOneChildController:home title:@"首页" norImage:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
        
        // 2.消息
         TTMessageViewController *message = [[TTMessageViewController alloc] init];
         [self addOneChildController:message title:@"消息" norImage:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
        // 3.广场
        TTDiscoverViewController *discover = [[TTDiscoverViewController alloc] init];

        [self addOneChildController:discover title:@"广场" norImage:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
         // 4.我
         TTProfileViewController *profile = [[TTProfileViewController alloc] init];
        NSString *norImageName = @"tabbar_profile";
        NSString *selImageName = @"tabbar_profile_selected";
        [self addOneChildController:profile title: @"我" norImage:norImageName selectedImage:selImageName];
        
    }
    return self;
}

/**
 *  初始化子控制器
 *
 *  @param childVc       需要初始化的子控制器
 *  @param title         控制器的标题
 *  @param norImage      控制器的默认状态的图片
 *  @param selectedImage 控制器的选中状态的图片
 */
- (void)addOneChildController:(UIViewController *)childVc title:(NSString *)title norImage:(NSString *)norImage selectedImage:(NSString *)selectedImage
{

//    childVc.view.backgroundColor = IWRandomColor;
    childVc.title = title;
    
    childVc.tabBarItem.image = [UIImage imageWithName:norImage];
    UIImage *selImage = [UIImage imageWithName:selectedImage];
    
    // 告诉系统不要按照tintColor渲染图片
    // 判断当前是否是ios7
    if (iOS7) {
        selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    }
    childVc.tabBarItem.selectedImage = selImage;
    // 包装一个导航条
    TTNavigationController  *nav = [[TTNavigationController alloc] initWithRootViewController:childVc];
    
    [self addChildViewController:nav];
    
    // 添加一个自定义选项卡按钮
    [self.customTabBar addTabBarButton:childVc.tabBarItem];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.创建自定义tabBar
    TTTabBar *customTabBar = [[TTTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
    
    // 设置代理
    self.customTabBar.delegate = self;

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 遍历tabbar中所有的子控件, 删除不需要的控件
    // UITabBarButton 这个类是私有API,
    for (UIView *subView in self.tabBar.subviews) {
        if ([subView isKindOfClass:[UIControl class]]) {
            [subView removeFromSuperview];
        }
    }
}

#pragma mark - IWTabBarDelegate
- (void)tabBar:(TTTabBar *)tabBar from:(NSInteger)from to:(NSInteger)to
{
    // 切换子控制器
    self.selectedIndex = to;
}

@end
